package com.qhgrain.app.controller;

import java.util.Arrays;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.qhgrain.permission.constant.ShiroConstant;
import com.qhgrain.permission.realm.UserRealm;

@RequestMapping("/")
@Controller
public class IndexController {

	@Autowired
	private UserRealm userRealm;
	
	@RequestMapping("/index")
	public String index(HttpServletRequest request,Model model){
		 Object object =  SecurityUtils.getSubject().getSession().getAttribute(ShiroConstant.CURRENT_USER);
	     model.addAttribute("userInfo", object);
	     //打印缓存权限
	     Collection<String> roles = userRealm.getUserAuthorizationInfo().getRoles();
	     Collection<String> permissions = userRealm.getUserAuthorizationInfo().getStringPermissions();
	     System.out.println(object);
	     System.out.println("角色="+(roles != null?Arrays.deepToString(roles.toArray()):""));
	     System.out.println("权限="+(permissions != null?Arrays.deepToString(permissions.toArray()):""));
		 System.out.println("用户名："+request.getAttribute("iamMessage"));	
	     return "index";
	}
	
	public static String getLoginName(){
		 Object object =  SecurityUtils.getSubject().getPrincipal();
		 System.out.println("用户登录名=" + (object == null?"":(String)object));
	     //获取登录名
	     if(object != null ){
	    	 return (String)object;
	     }
	     return null;
	}
}
